package com.example.demo.controler;

import com.example.demo.utils.WangyiUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
public class Wangyiuploader {
    @GetMapping("/2")
    public void upload(HttpServletResponse response) throws Exception {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "D:\\ChromeDriver\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("download.default_directory", "D:\\360MoveData\\Users\\疯子似的少年\\Desktop\\自媒体");
        options.setExperimentalOption("prefs", prefs);
        ChromeDriver webDriver = new ChromeDriver(options);
        Set<Cookie> read = WangyiUtils.read();
        String url = "https://jianwai.youdao.com/audioTransfer";
        webDriver.get(url); //
        webDriver.manage().deleteAllCookies();
        for (Cookie cookie : read) {
            webDriver.manage().addCookie(cookie);
        }
        // 与浏览器同步非常重要，必须等待浏览器加载完毕
        webDriver.navigate().refresh();
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //刚加载容易出错,若还没加载完,就继续加载
        String windowHandle = webDriver.getWindowHandle();
        while (true) {
            try {
                //这里会返回一个webdriver frame对象,用这个对象操作这个frame里的元素才行(栽过坑)
                webDriver.switchTo().frame(webDriver.findElement(By.cssSelector("iframe[scrolling]")));
                webDriver.findElement(By.cssSelector("input[data-type]")).sendKeys("1124209551@qq.com");
                webDriver.findElement(By.cssSelector("input[name=password]")).sendKeys("maw282814...");
                Actions action = new Actions(webDriver);
                action.sendKeys(Keys.ENTER).perform();
                Thread.sleep(3000);
                break;
            } catch (Exception e) {
                System.out.println("页面还没加载完毕");
                webDriver.switchTo().window(windowHandle);
                Thread.sleep(500);
            }
        }
        webDriver.get("https://jianwai.youdao.com/audioTransfer");
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        while (true) {
            try {
                webDriver.findElementByXPath("//*[@id=\"app\"]/div[1]/div/div[2]/div[1]/div/input").sendKeys("字幕");
                webDriver.findElementByXPath("//*[@id=\"app\"]/div[1]/div/div[2]/div[3]/div/div").click();
                Thread.sleep(500);
                webDriver.findElementByXPath("//*[@id=\"app\"]/div[1]/div/div[2]/div[3]/div/ul/li[2]").click();

                webDriver.findElementByXPath("//*[@id=\"app\"]/div[1]/div/div[2]/div[4]/div/div").click();
                Thread.sleep(500);
                webDriver.findElementByXPath("//*[@id=\"app\"]/div[1]/div/div[2]/div[4]/div/ul/li[2]").click();

                webDriver.findElementByXPath("//*[@id=\"app\"]/div[1]/div/div[2]/div[2]/div/div/input").click();
                Thread.sleep(3000);
                break;

            } catch (Exception e) {
                System.out.println("页面还没加载完呢");
                Thread.sleep(500);
            }
        }
        while (true) {
            try {
                Process exec = Runtime.getRuntime().exec("D:\\360MoveData\\Users\\疯子似的少年\\Desktop\\自媒体\\网易上传.exe");
                break;
            } catch (Exception e) {
                System.out.println("出错");
                Thread.sleep(1000);
            }

        }


        while (true) {
            //判断文件是否上传完
            try {
                Thread.sleep(6000);
                String text = webDriver.findElementByXPath("//*[@id=\"app\"]/div[1]/div/div[2]/div[2]/div/div/span[3]").getText();
                if (text != null) {
                    webDriver.findElementByXPath("//*[@id=\"app\"]/div[1]/div/div[3]").click();
                    break;
                }
            } catch (Exception e) {
                System.out.println("还没加载完");
            }
        }

        while (true) {
            //判断是否制作完成
            try {
                Thread.sleep(6000);
                webDriver.navigate().refresh();
                WebElement elementByCssSelector = webDriver.findElementByCssSelector("span.cover");
            } catch (Exception e) {
                System.out.println("没捕获到,说明制作完成");
                break;
            }
        }

        webDriver.findElements(By.cssSelector("img[data-v-e71efcea]")).get(0).click();
        while (true) {
            try {
                Thread.sleep(3000);
                webDriver.findElementByXPath("//*[@id=\"app\"]/div[1]/div[2]/div/div[1]/div[1]/div[4]").click();
                break;
            } catch (Exception e) {
                System.out.println("网页跳转还没加载完呢");
            }
        }
        response.getOutputStream().write("成功".getBytes());
    }

    @GetMapping("/1")
    public void huoqu(HttpServletResponse response) throws Exception {
        File file = new File("D:\\360MoveData\\Users\\疯子似的少年\\Documents\\WeChat Files\\wxid_uhvd1hes7cod22\\Applet\\wx624a2bc21898adb9\\temp");
        File[] files = file.listFiles();
        ArrayList<File> files1 = new ArrayList<>();
        for (File file1 : files) {
            if (file1.getName().length() < 10) {
                files1.add(file1);
            }
        }
        Collections.sort(files1, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                String s1 = o1.getName().replaceAll(".txt", "");
                String s2 = o2.getName().replaceAll(".txt", "");
                int i = Integer.parseInt(s1);
                int i1 = Integer.parseInt(s2);
                return i1 - i;
            }
        });
        FileInputStream fileInputStream = new FileInputStream(files1.get(0));
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\360MoveData\\Users\\疯子似的少年\\Desktop\\自媒体\\license.mp3");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        byte[] bytes = new byte[1024];
        int count = 0;
        while ((count = bufferedInputStream.read(bytes)) != -1) {
            bufferedOutputStream.write(bytes);
        }
        //必须加,否则数据默认在缓冲区,没用啦
        bufferedInputStream.close();
        bufferedOutputStream.close();
        files1.get(0).delete();
        System.out.println("完成");
    }

    @PostMapping("/posttest")
    public void posttest(@RequestParam("name") String value, HttpServletRequest request) {
        javax.servlet.http.Cookie[] cookies = request.getCookies();
        for (javax.servlet.http.Cookie cookie : cookies) {
            System.out.println(cookie.toString());
            System.out.println(cookie.getName());
            System.out.println(cookie.getValue());
            System.out.println(cookie.getDomain());
        }
        System.out.println(value);
    }

    @GetMapping("/3")
    public void choulaTiao() throws InterruptedException {
        System.out.println(777777);
        //先讀取
        while (true) {
            Thread.sleep(3000);
            System.out.println("789");
        }
    }
    @GetMapping("/5")
    public void  t1(){
        System.out.println("5");
    }

}
